package com.jkdz.spyke.widgetUIs {
	import spark.components.Button;
	import spark.components.Form;
	import spark.components.FormItem;
	import spark.components.Group;
	import spark.components.TextArea;
	import spark.layouts.HorizontalAlign;
	import spark.layouts.HorizontalLayout;
	import spark.layouts.VerticalAlign;
	import spark.layouts.VerticalLayout;

	import com.jkdz.spyke.flexUI.helpers.Link;
	import com.jkdz.spyke.flexUI.helpers.LocalPropertyListener;
	import com.jkdz.spyke.flexUI.helpers.MouseWheelManager;
	import com.jkdz.spyke.flexUI.helpers.SuperWindow;
	import com.jkdz.spyke.flexUI.main.Main;
	import com.jkdz.spyke.rs.SpykeChatRoom;
	import com.jkdz.spyke.rs.SpykeChatRoomListener;

	import mx.binding.utils.BindingUtils;
	import mx.controls.TextInput;
	import mx.core.FlexGlobals;
	import mx.events.FlexEvent;

	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class SpykeChatRoomUI extends SpykeWidgetUI implements SpykeChatRoomListener, LocalPropertyListener {
		
		private var widget:SpykeChatRoom;
		private var window:SuperWindow;
		private var nameLink:Link;
		private var area:TextArea;
		
		public function SpykeChatRoomUI(widget:SpykeChatRoom) {
			this.widget = widget;
		}
		
		public function roomNameChanged(newName:String):void {
			window.title = newName;
		}
		
		override public function display(onComplete:Function):void {
			
			widget.getName(function(name:String):void {
				showUI(name, onComplete);
			});
		}
		
		public function newMessageArrived(message:String):void {
			area.appendText("\n"+message);
		}
		
		public function userNameChanged(name:String):void {
			nameLink.text = name+": ";
		}
		
		public function unPublished():void {
			if (window.isVisible()) {
				(Main)(FlexGlobals.topLevelApplication).removeLocalPropertyListener(this);
				window.close();
			}
		}
		
		private function showUI(name:String, onComplete:Function):void {
			
			var vl:VerticalLayout = new VerticalLayout();
			vl.gap = 10;
			vl.horizontalAlign = VerticalAlign.MIDDLE;
			vl.paddingBottom = vl.paddingTop = vl.paddingRight = vl.paddingLeft = 10;
			this.layout = vl;
			
			area = new TextArea();
			area.editable = false;
			area.text = "Welcome!";
			this.addElement(area);
			
			var hg:Group = new Group();
			var hl:HorizontalLayout = new HorizontalLayout();
			hl.gap = 10;
			hl.verticalAlign = VerticalAlign.MIDDLE;
			hg.layout = hl;
			
			nameLink = new Link((Main)(FlexGlobals.topLevelApplication).getUserName()+": ");
			nameLink.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
				showChangeNameDialog();	
			});
			nameLink.toolTip = "Click here to edit SpykeChatRoom settings.";
			hg.addElement(nameLink);
			
			var line:TextInput = new TextInput();
			line.selectable = true;
			line.editable = true;
			line.toolTip = "Type the message here.";
			line.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void {
				if (e.charCode == 13 && line.text.length > 0) {
					widget.sendMessage(nameLink.text + line.text);
					line.text = "";
				}
			});
			hg.addElement(line);
			
			this.addElement(hg);
			
			this.window = SuperWindow.display(FlexGlobals.topLevelApplication as Main, this, name, false, true, true, true, new Point(100+Math.random()*((FlexGlobals.topLevelApplication as Main).width-500), 100+Math.random()*((FlexGlobals.topLevelApplication as Main).height-500)), onClose);
			window.setSizeConstraints(new Rectangle(200, 120, 10000, 10000));
		
			window.addEventListener(FlexEvent.CREATION_COMPLETE, function(e:Event):void {
				var adjuster:Function = function(v:Number):void {
					line.width = window.width-32-nameLink.width;
					area.width = window.width-22;
					area.height = window.height-74-nameLink.height;
				};
				BindingUtils.bindSetter(adjuster, window, "width");
				BindingUtils.bindSetter(adjuster, window, "height");
				BindingUtils.bindSetter(adjuster, nameLink, "width");
				
				window.width = 500;
				window.height = 300;
			});
			
			(Main)(FlexGlobals.topLevelApplication).addLocalPropertyListener(this);
			
			MouseWheelManager.addMouseWheelListener(this, function(atX:int, atY:int, delta:int):void {
				area.scroller.verticalScrollBar.value -= 20*delta;
			});
			
			widget.registerListener(this);
			
			onComplete();
		}
		
		private function onClose():void {
			widget.unPublish();
			(Main)(FlexGlobals.topLevelApplication).removeLocalPropertyListener(this);
			MouseWheelManager.removeMouseWheelListener(this);
		}
		
		private function showChangeNameDialog():void {
			var crfg:Group = new Group();
			var vl:VerticalLayout = new VerticalLayout();
			vl.horizontalAlign = HorizontalAlign.CENTER;
			vl.paddingBottom = 10.0;
			crfg.layout = vl;
			var crff:Form = new Form();
			var crffi:FormItem = new FormItem();
			var crfti:TextInput = new TextInput();
			var crfb:Button = new Button();
			crfb.label = "OK";
			var submit:Function = function():void {
				if (crfti.text.length == 0 || rrfti.text.length == 0) {
					SuperWindow.alert(FlexGlobals.topLevelApplication as Main, "Wrong name", "The name cannot be empty.", function():void {
						cnw.close();
					});
				} else {
					nameLink.text = crfti.text+": ";
					(Main)(FlexGlobals.topLevelApplication).setUserName(crfti.text);
					widget.renameRoom(rrfti.text);
					cnw.close();
				}
			};
			crfb.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
				submit();
			});
			crffi.label = "Your name:";
			crffi.addElement(crfti);
			crff.addElement(crffi);
			crfti.text = nameLink.text.substr(0, nameLink.text.length-2);
			crfti.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void {
				if (e.charCode == 13) {
					submit();
				}
			});
			crfg.addElement(crff);
			
			var rrffi:FormItem = new FormItem();
			var rrfti:TextInput = new TextInput();
			rrffi.label = "Chat name:";
			rrffi.addElement(rrfti);
			crff.addElement(rrffi);
			rrfti.text = window.title;
			rrfti.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void {
				if (e.charCode == 13) {
					submit();
				}
			});
			
			crfg.addElement(crfb);
			var cnw:SuperWindow = SuperWindow.display((Main)(FlexGlobals.topLevelApplication), crfg, "SpykeChatRoom settings", true, true, true, false);
			focusManager.setFocus(crfti);
		}

	}
}